package com.xltt.watchlauncher3.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.xltt.watchlauncher3.R;
import com.xltt.watchlauncher3.activity.MainActivity;
import com.xltt.watchlauncher3.adapter.NoticationListAdapter;
import com.xltt.watchlauncher3.entity.NoticationEntity;
import com.xltt.watchlauncher3.utils.AsyncTaskUtils;
import com.xltt.watchlauncher3.utils.Logger;
import com.xltt.watchlauncher3.view.button.TopToBottomLayout;
import com.xltt.watchlauncher3.view.button.TopToBottomlListener;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class ClockDialFragment extends BaseFragment{
    private View view;
    private TopToBottomLayout mTopToBottomLayout;
    private TextView mCleanNoticationBtn;
    private ListView mNoticationListView;
    private NoticationListAdapter mNoticationListAdapter;
    private AsyncTaskUtils mAsyncTaskUtils;
    @Override
    protected View initView(LayoutInflater inflater) {
        Logger.d("表盘界面创建 onViewCreated()");
        view = inflater.inflate(R.layout.clock_dial_fragment, null);
        init(view);
        return view;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Logger.d("onActivityCreated()");
        mAsyncTaskUtils = new AsyncTaskUtils<>();
        mAsyncTaskUtils.setTaskListener(new AsyncTaskUtils.TaskListener() {
            @Override
            public void onPreExecute() {
                Logger.d("准备加载通知数据");
            }

            @Override
            public void onProgressUpdate(int progress) {

            }

            @Override
            public Object doInBackground(Object o) {
                List<NoticationEntity> list = new ArrayList<>();
//                list.add(new NoticationEntity("通知title","通知内容",System.currentTimeMillis()));
                return list;
            }

            @Override
            public void onPostExecute(Object o) {
                List<NoticationEntity> list = (List<NoticationEntity>)o;
                Logger.d("完成加载通知数据,size = " + list.size());
                mCleanNoticationBtn.setVisibility(list.size() == 0 ? View.GONE : View.VISIBLE);
                mNoticationListAdapter.setData(list);
                mNoticationListAdapter.notifyDataSetChanged();
            }
        });
        mAsyncTaskUtils.execute("");
    }

    private void init(View view) {
        Logger.d("onViewCreated()");
        //1.下拉通知按钮
        mTopToBottomLayout = view.findViewById(R.id.drop_down_view);
        mTopToBottomLayout.setToolbarPanelListener(new TopToBottomlListener() {
            @Override
            public void onPanelSlide(Toolbar toolbar, View panelView, float slideOffset) {

            }

            @Override
            public void onPanelOpened(Toolbar toolbar, View panelView) {
                ((MainActivity)getActivity()).setViewPagerSlide(true);
            }

            @Override
            public void onPanelClosed(Toolbar toolbar, View panelView) {
                ((MainActivity)getActivity()).setViewPagerSlide(false);
            }
        });
        //2.下拉通知后的清除按钮
        mCleanNoticationBtn = mTopToBottomLayout.findViewById(R.id.clear_notication_btn);
        mCleanNoticationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTopToBottomLayout.closePanel();
            }
        });
        //3.通知列表
        mNoticationListView = mTopToBottomLayout.findViewById(R.id.notication_list);
        mNoticationListAdapter = new NoticationListAdapter(getActivity());
        mNoticationListView.setAdapter(mNoticationListAdapter);
        mNoticationListView.setEmptyView(mTopToBottomLayout.findViewById(R.id.empty_notication));
    }

    @Override
    public void onPause() {
        Logger.d("onPause()");
        super.onPause();
    }

    @Override
    public void onResume() {
        Logger.d("onResume()");
        super.onResume();
    }

    @Override
    public void onDestroy() {
        Logger.d("onDestroy()");
        if(mAsyncTaskUtils != null){
            mAsyncTaskUtils.cancle();
            mAsyncTaskUtils = null;
        }
        super.onDestroy();
    }
}
